package com.java110.mall.openapi.bmo.impl;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.factory.GenerateCodeFactory;
import com.java110.dto.gold.GoldDto;
import com.java110.intf.acct.IGoldV1InnerServiceSMO;
import com.java110.mall.openapi.bmo.IMallCommonApiBmo;
import com.java110.po.gold.GoldPo;
import com.java110.utils.util.Assert;
import com.java110.utils.util.ListUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("queryPropertyGoldBmoImpl")
public class QueryPropertyGoldBmoImpl implements IMallCommonApiBmo {

    @Autowired
    private IGoldV1InnerServiceSMO goldV1InnerServiceSMOImpl;

    @Override
    public void validate(ICmdDataFlowContext context, JSONObject reqJson) {
        Assert.hasKeyAndValue(reqJson, "communityId", "未包含小区");
        Assert.hasKeyAndValue(reqJson, "communityName", "未包含小区名称");
        Assert.hasKeyAndValue(reqJson, "storeName", "未包含物业名称");
        Assert.hasKeyAndValue(reqJson, "storeId", "未包含物业ID");
    }

    @Override
    public void doCmd(ICmdDataFlowContext context, JSONObject reqJson) {

        GoldDto goldDto = new GoldDto();
        goldDto.setCommunityId(reqJson.getString("communityId"));
        List<GoldDto> goldDtos = goldV1InnerServiceSMOImpl.queryGolds(goldDto);
        if (ListUtil.isNull(goldDtos)) {
            GoldPo goldPo = new GoldPo();
            goldPo.setAmount("0");
            goldPo.setGoldName(reqJson.getString("communityName"));
            goldPo.setGoldId(GenerateCodeFactory.getGeneratorId("11"));
            goldPo.setCommunityName(reqJson.getString("communityName"));
            goldPo.setStoreName(reqJson.getString("storeName"));
            goldPo.setCommunityId(reqJson.getString("communityId"));
            goldPo.setStoreId(reqJson.getString("storeId"));
            goldV1InnerServiceSMOImpl.saveGold(goldPo);
            goldDto = new GoldDto();
            goldDto.setCommunityId(reqJson.getString("communityId"));
            goldDtos = goldV1InnerServiceSMOImpl.queryGolds(goldDto);
        }

        context.setResponseEntity(ResultVo.createResponseEntity(goldDtos));

    }
}
